package com.yuncool.config;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.collect.Sets;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther XIE
 * @Date 2019/9/2 15:48
 * @Description TODO
 * @Version 1.0
 */
@Component
public class MySwaggerApi implements ApiListingScannerPlugin {
	@Override
	public List<ApiDescription> apply(DocumentationContext documentationContext) {
		return new ArrayList<>(
			Arrays.asList(
				new ApiDescription(
					"/auth/login",  //url
					"UserLogin", //描述
					Arrays.asList(
						new OperationBuilder(
							new CachingOperationNameGenerator())
							.method(HttpMethod.POST)//http请求类型
							.produces(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE))
							.summary("用户登陆")
							.notes("用户登陆")//方法描述
							.tags(Sets.newHashSet("用户权限系统"))//归类标签
							.parameters(
								Arrays.asList(
									new ParameterBuilder()
										.description("用户名")
										.type(new TypeResolver().resolve(String.class))
										.name("username")
										.parameterType("query")
										.parameterAccess("access")
										.required(true)
										.modelRef(new ModelRef("string")) //<5>
										.build(),
									new ParameterBuilder()
										.description("密码")
										.type(new TypeResolver().resolve(String.class))
										.name("password")
										.parameterType("query")
										.parameterAccess("access")
										.required(true)
										.modelRef(new ModelRef("string")) //<5>
										.build()
								))
							.build()),
					false)));
	}


	@Override
	public boolean supports(DocumentationType documentationType) {
		return DocumentationType.SWAGGER_2.equals(documentationType);
	}
}
